package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.DrugInfo;
import com.ruoyi.project.drug.service.IDrugInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/drug/info")
public class DrugInfoController extends BaseController {
    @Autowired
    private IDrugInfoService iDrugInfoService;

    @PreAuthorize("@ss.hasPermi('drug:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrugInfo drugInfo)
    {
        startPage();
        List<DrugInfo> list = iDrugInfoService.selectDrugInfoList(drugInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('drug:info:add')")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DrugInfo drugInfo)
    {
        drugInfo.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iDrugInfoService.insertDrugInfo(drugInfo));
    }

    @PreAuthorize("@ss.hasPermi('drug:info:remove')")
    @Log(title = "药品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugIds}")
    public AjaxResult remove(@PathVariable Long[] drugIds)
    {
        return toAjax(iDrugInfoService.deleteDrugInfo(drugIds));
    }


    @PreAuthorize("@ss.hasPermi('drug:info:edit')")
    @Log(title = "药品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DrugInfo drugInfo)
    {
        drugInfo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iDrugInfoService.updateDrugInfo(drugInfo));
    }

    @PreAuthorize("@ss.hasPermi('drug:info:query')")
    @GetMapping(value = "/{drugId}")
    public AjaxResult getInfo(@PathVariable Long drugId)
    {
        return AjaxResult.success(iDrugInfoService.selectDrugInfoById(drugId));
    }
}
